package UI;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import threads.BaseRunnableBCBS;
import Logic.SQLReader;
import UI.MainFrame.LogOut;
import UI.BaseModels.BCBSPanel;
import UI.BaseModels.HeaderLabel;

import java.awt.Color;

import java.sql.SQLException;

import javax.swing.ImageIcon;

/**
 * The Class MainMenu.
 */
public class MainMenu extends BCBSPanel {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 4974501530632409572L;

	/** The current username label. */
	private HeaderLabel currentUsernameLabel;

	/** The current balance. */
	public JLabel currentBalance;

	/** The feedbackline1. */
	public JLabel feedbackline1;

	/** The feedbackline2. */
	public JLabel feedbackline2;

	/** The lbl new label_1. */
	private JLabel lblNewLabel_1;
	
	private JLabel exchangeRateLabel;

	/**
	 * Instantiates a new main menu.
	 */
	public MainMenu() {

		JLabel lblNewLabel = new JLabel("Balance:");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblNewLabel.setBounds(111, 124, 89, 19);
		this.add(lblNewLabel);

		JButton sellBCBS_button = new JButton("Sell BCBS");
		sellBCBS_button.setBounds(10, 253, 136, 43);
		sellBCBS_button.addActionListener(new BtnSwitchToSellBCBS());
		this.add(sellBCBS_button);
		sellBCBS_button.setFont(new Font("Tahoma", Font.PLAIN, 13));

		currentBalance = new JLabel("1");
		currentBalance.setFont(new Font("Tahoma", Font.BOLD, 15));
		currentBalance.setHorizontalAlignment(SwingConstants.CENTER);
		currentBalance.setBounds(121, 154, 72, 19);
		this.add(currentBalance);
		
		exchangeRateLabel = new JLabel("");
		exchangeRateLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
		exchangeRateLabel.setHorizontalAlignment(SwingConstants.CENTER);
		exchangeRateLabel.setBounds(0, 399, 305, 19);
		this.add(exchangeRateLabel);

		currentUsernameLabel = new HeaderLabel("Firstname");
		currentUsernameLabel.setHorizontalAlignment(SwingConstants.LEFT);
		this.add(currentUsernameLabel);

		JButton btnViewTransactions = new JButton("Transactions");
		btnViewTransactions.setBounds(162, 253, 136, 43);
		this.add(btnViewTransactions);
		btnViewTransactions.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnViewTransactions.addActionListener(new BtnSwitchToTransactions());

		JButton transferBCBS_button = new JButton("Transfer BCBS");
		transferBCBS_button.setBounds(162, 199, 136, 43);
		transferBCBS_button.addActionListener(new BtnSwitchToTransferBCBS());

		JButton purchaseBCBS_button = new JButton("Purchase BCBS");
		purchaseBCBS_button.setBounds(10, 199, 136, 43);
		this.add(purchaseBCBS_button);
		purchaseBCBS_button.addActionListener(new BtnSwitchToPurchaseBCBS());
		purchaseBCBS_button.setFont(new Font("Tahoma", Font.PLAIN, 13));
		this.add(transferBCBS_button);
		transferBCBS_button.setFont(new Font("Tahoma", Font.PLAIN, 13));

		JButton logout_button = new JButton("Log Out");
		logout_button.setBounds(99, 316, 106, 34);
		this.add(logout_button);
		logout_button.addActionListener(new LogOut());
		logout_button.setFont(new Font("Tahoma", Font.PLAIN, 13));

		feedbackline1 = new JLabel();
		feedbackline1.setForeground(new Color(0, 128, 0));
		feedbackline1.setFont(new Font("Tahoma", Font.BOLD, 13));
		feedbackline1.setHorizontalAlignment(SwingConstants.CENTER);
		feedbackline1.setBounds(10, 58, 285, 26);
		feedbackline2 = new JLabel();
		feedbackline2.setHorizontalAlignment(SwingConstants.CENTER);
		feedbackline2.setForeground(new Color(0, 128, 0));
		feedbackline2.setFont(new Font("Tahoma", Font.BOLD, 13));
		feedbackline2.setSize(285, 26);
		feedbackline2.setLocation(10, 78);
		this.add(feedbackline1);
		this.add(feedbackline2);

		JButton edit_selfButton = new JButton("Edit Profile");
		edit_selfButton.setFont(new Font("Tahoma", Font.PLAIN, 13));
		edit_selfButton.setBounds(189, 11, 106, 34);
		edit_selfButton.addActionListener(new BtnSwitchToEditSelf());
		this.add(edit_selfButton);

		feedbackLabel.setLocation(0, 362);

		lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(new ImageIcon(MainMenu.class
				.getResource("/Programmeringsbilleder/Baggrund.jpg")));
		lblNewLabel_1.setBounds(0, 0, 312, 446);
		add(lblNewLabel_1);

	}

	private class BtnSwitchToPurchaseBCBS implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			UI.MainFrame.switchToPanel(UI.MainFrame.purchase_BCBS, null);
		}
	}


	private class BtnSwitchToTransferBCBS implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			UI.MainFrame.switchToPanel(UI.MainFrame.transfer_BCBS, null);
		}
	}


	private class BtnSwitchToTransactions implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			UI.MainFrame.switchToPanel(UI.MainFrame.transactions, null);
		}
	}


	private class BtnSwitchToSellBCBS implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			UI.MainFrame.switchToPanel(UI.MainFrame.sell_BCBS, null);
		}
	}


	private class BtnSwitchToEditSelf implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			UI.MainFrame.switchToPanel(UI.MainFrame.edit_self, null);
		}
	}

	/**
	 * Sets the header to the first name of the current user (supplied as parameter).
	 * 
	 * @param header
	 *            The first name of the user.
	 */
	public void setHeader(String header) {
		currentUsernameLabel.setText(header);
	}

	/**
	 * Feedback for the main menu panel. This has two lines and hence takes two arguments, unlike the default feedback which takes one.
	 * 
	 * @param feedback1
	 *            Feedback string of line one.
	 * @param feedback2
	 *            Feedback string of line two.
	 * @param value
	 *            The color of the labels - green if true, red if false.
	 */
	public void feedback(String feedback1, String feedback2, boolean value) {
		if (value) {
			feedbackline1.setText(feedback1);
			feedbackline1.setForeground(feedbackGreen);
			feedbackline2.setText(feedback2);
			feedbackline2.setForeground(feedbackGreen);
		} else {
			feedbackline1.setText(feedback1);
			feedbackline1.setForeground(feedbackRed);
			feedbackline2.setText(feedback2);
			feedbackline2.setForeground(feedbackRed);
		}

	}

	/**
	 * Clears both the feedback-labels in the top of the panel.
	 */
	public void clearFeedbackTop() {
		feedbackline1.setText("");
		feedbackline2.setText("");
	}

	/**
	 * Creates a new Runnable of the type UpdateMainMenuRunnable, sets it to the current
	 * runnable for the panel, places it in its own thread and runs it.
	 * whatever it's doing first.
	 */
	public void refreshInfo() {
		runnable = new UpdateMainMenuRunnable();
		Thread t = new Thread(runnable);
		t.start();
	}

	/**
	 * See the run-method of this class for details.
	 * 
	 */
	private class UpdateMainMenuRunnable extends BaseRunnableBCBS {
		/**
		 * Refreshes the interface with current balance and current username based on info fetched from SQL.
		 */
		@Override
		public void run() {
			SQLReader reader = new SQLReader();
			feedback("Refreshing....", true);
			if (!stop) {
				try {
					exchangeRateLabel.setText("Exchange rate: " + String.valueOf(reader.getExchangeRate()));
					UI.MainFrame.currentUser
							.setBalance(reader
									.getUpdatedBalance(UI.MainFrame.currentUser
											.getId()));
					if (!stop) {
						currentBalance
								.setText(String
										.valueOf(UI.MainFrame.currentUser
												.getBalance()));
						setHeader(UI.MainFrame.currentUser.getFirstname());
						clearFeedback();

					}
				} catch (SQLException e) {
					if (!stop) {
						feedback("Connection error!", false);
					}
				}
			}
		}
	}
}
